﻿//-----------------------------------------------------------------------
// <copyright file="PivotTableColumnCollection.cs" company="Forebrain Technology, LLC">
//
// Copyright (c) Forebrain Technology, LLC. All rights reserved.
// 
// This source code is released under the Microsoft Public License (Ms-PL).
//
// </copyright>
//-----------------------------------------------------------------------
namespace PivotCollectionTools.Data
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using PivotCollectionTools.Xml;

    /// <summary>
    /// A collection of Pivto Table Columns.
    /// </summary>
    [Serializable]
    public class PivotTableColumnCollection : ReadOnlyCollection<PivotTableColumn>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PivotTableColumnCollection"/> class.
        /// </summary>
        /// <param name="list">The list to wrap.</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// <paramref name="list"/> is null.
        /// </exception>
        public PivotTableColumnCollection(IList<PivotTableColumn> list)
            : base(list)
        {
        }

        /// <summary>
        /// Gets all column names.
        /// </summary>
        /// <value>All column names of the pivot table column collection.</value>
        public Collection<string> AllColumnNames
        {
            get
            {
                Collection<string> results = new Collection<string>();
                foreach (var column in this.Items)
                {
                    results.Add(column.Name);
                }

                return results;
            }
        }

        /// <summary>
        /// Gets the data bound column names.
        /// </summary>
        /// <value>
        /// The data bound column names of the pivot table column collection.
        /// </value>
        public Collection<string> DataBoundColumnNames
        {
            get
            {
                Collection<string> results = new Collection<string>();
                foreach (var column in this.Items.Where(c => c.Category.DataBound))
                {
                    results.Add(column.Name);
                }

                return results;
            }
        }

        /// <summary>
        /// Gets the <see cref="PivotCollectionTools.Data.PivotTableColumn"/> with the specified column name.
        /// </summary>
        /// <param name="columnName">The column name to get.</param>
        public PivotTableColumn this[string columnName]
        {
            get { return this.Items.FirstOrDefault(c => c.Name == columnName); }
        }

        /// <summary>
        /// Indexes the of the column.
        /// </summary>
        /// <param name="column">The column.</param>
        /// <returns>The index of the specified column.</returns>
        public int IndexOf(string column)
        {
            if (this[column] == null)
            {
                return -1;
            }

            return this.Items.IndexOf(this[column]);
        }

        /// <summary>
        /// Determines whether the column collection contains the specified colum name.
        /// </summary>
        /// <param name="columName">Name of the colum.</param>
        /// <returns>
        /// <c>true</c> if the column collection contains the specified colum name; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(string columName)
        {
            return this.Items.FirstOrDefault(c => c.Name == columName) != null;
        }
    }
}
